package com.paperobot.action;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.model.PaperType;
import com.paperobot.service.PaperobotService;

public class PaperTypeAction extends BaseAction{

	private static final long serialVersionUID = 1516834338392982332L;
	private PaperobotService paperobotService;;
	private PaperType paperType;
	private List<PaperType> paperTypeList = new ArrayList<PaperType>();
	private PageUtil pageUtil = new PageUtil();
	
	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	public PaperType getPaperType() {
		return paperType;
	}
	public void setPaperType(PaperType paperType) {
		this.paperType = paperType;
	}
	public List<PaperType> getPaperTypeList() {
		return paperTypeList;
	}
	public void setPaperTypeList(List<PaperType> paperTypeList) {
		this.paperTypeList = paperTypeList;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	
	@SuppressWarnings("unchecked")
	public String listPaperType(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			
			List<PaperType> allPaperTypeList = paperobotService.getPaperTypeList();
			paperTypeList = pageUtil.getCurrentPageRows(pagetype, allPaperTypeList.size(), allPaperTypeList);
			
			return SUCCESS;
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String updatePaperType(){
		try {
			String paperTypeId = request.getParameter("paperTypeId");
			paperType = paperobotService.getPaperType(Long.parseLong(paperTypeId));
			return "update";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String deletePaperType(){
		try {
			String paperTypeId = request.getParameter("paperTypeId");
			int rs = paperobotService.deletePaperType(Long.parseLong(paperTypeId));
			if(rs > 0){
				return "delete";
			}
			return ERROR;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String addPaperType(){

		 try {
			String type = request.getParameter("type");
			 String typeName = request.getParameter("typeName");
			 String subType = request.getParameter("subType");
			 String subTypeName = request.getParameter("subTypeName");
			      
			 PaperType paperType = new PaperType();
			 paperType.setType(Integer.parseInt(type));
			 paperType.setTypeName(typeName);
			 paperType.setSubType(Integer.parseInt(subType));
			 paperType.setSubTypeName(subTypeName);
			 
			 if(paperobotService.addPaperType(paperType) > 0){
			 	logger.info("添加类别成功。");
			 	request.setAttribute("result", "添加类别成功！");
			 	return SUCCESS;
			 }else{
				logger.info("添加类别失败。");
			 	request.setAttribute("result", "添加类别失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	
	}

	public String savePaperType(){
		 try {
			String paperTypeId = request.getParameter("paperTypeId");
			 String type = request.getParameter("type");
			 String typeName = request.getParameter("typeName");
			 String subType = request.getParameter("subType");
			 String subTypeName = request.getParameter("subTypeName");
			 
			 PaperType paperType = new PaperType();
			 paperType.setPaperTypeId(Long.parseLong(paperTypeId));
			 paperType.setType(Integer.parseInt(type));
			 paperType.setTypeName(typeName);
			 paperType.setSubType(Integer.parseInt(subType));
			 paperType.setSubTypeName(subTypeName);
			 
			 if(paperobotService.updatePaperType(paperType) > 0){
			 	logger.info("更新类别成功。");
			 	request.setAttribute("result", "更新类别成功！");
			 	return SUCCESS;
			 }else{
				logger.info("更新类别失败。");
			 	request.setAttribute("result", "更新类别失败！");
			 	return ERROR;
			 }
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	
	
	
	
	
}
